package com.sinopec.handdevice.presenter

import com.sinopec.basemodule.BaseApplication
import com.sinopec.basemodule.model.UserInfo
import com.sinopec.basemodule.router.ReflectUtils
import com.sinopec.basemodule.utils.EncodingUtils
import com.sinopec.basemodule.utils.GsonUtils
import com.sinopec.basemodule.utils.NetUtil
import com.sinopec.basemodule.utils.WebServiceHelper
import com.sinopec.handdevice.contract.LoginContract
import okhttp3.Call
import okhttp3.Callback
import okhttp3.Response

/**
 * @author lwj
 * @ClassName: LoginActivity
 * @Description: 登录页面
 */
class LoginPresenter(view: LoginContract.View?) : LoginContract.Presenter(view) {
    override fun login(userPhone: String, password: String, deviceNo: String) {
//        val loginObservable = LoginApi.getInstance().login(userPhone, password, deviceNo)
//        toSubscribe(loginObservable, object : CommonObserver<UserInfo?>(mView.context) {
//            override fun onNext(userInfo: UserInfo) {
//                mView.showMessage("登录成功")
//                BaseApplication.getInstance().userManager.user = userInfo
//                ReflectUtils.navigationToHome(mView.context, 0)
//                mView.context.finish()
//            }
//
//            override fun onError(t: ResponseThrowable) {
//                //mView.showMessage(t.message)
//                mView.loginError(t.message)
//            }
//        })

        val userPhoneBase64 = EncodingUtils.encodeBase64(userPhone)
        val passwordBase64 = EncodingUtils.encodeBase64(password)
        val wifiIPAddress = NetUtil.getWifiIPAddress(mView.context)
        val iPAddress = NetUtil.getIPAddress()
        val client_ip = if (NetUtil.isWifiConnected(mView.context)) wifiIPAddress else iPAddress
        val paramsMap: HashMap<String, Any> = HashMap()
        paramsMap["userPhone"] = userPhoneBase64
        paramsMap["password"] = passwordBase64
        paramsMap["client_ip"] = client_ip

        WebServiceHelper.post("token", paramsMap, object : Callback {
            override fun onResponse(call: Call, response: Response) = if (response.isSuccessful) {
                //// 获取响应体内容
                val responseData = response.body.toString()
                //JSONObject jsonObject = new JSONObject(responseData)
                val resultMap = GsonUtils.GsonToMaps<Any>(responseData)
                val token = resultMap["access_token"].toString()
                BaseApplication.getInstance().updateToken(token)
                val userInfo = UserInfo()
                userInfo.userId = ""
                userInfo.userName = userPhone
                userInfo.userAccount = userPhone
                BaseApplication.getInstance().userManager.user = userInfo
                ReflectUtils.navigationToHome(mView.context, 0)
                mView.showMessage("登录成功")
                mView.context.finish()
            } else {
                // 处理错误情况，例如打印错误码和消息等
                mView.context.runOnUiThread() {
                    mView.loginError(response.message)
                }
                //测试数据
                val token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJyblN0ciI6IlNGMHBFUWp6MWRYc0swNkY0dExsd3NRbFN5cXhkUUVDIn0.VEq1Y_ZPeBeTR412szW2zjwFmcmdu4t8BJMz1YAtNHE"
                BaseApplication.getInstance().updateToken(token)
                val userInfo = UserInfo()
                userInfo.userId = ""
                userInfo.userName = userPhone
                userInfo.userAccount = userPhone
                BaseApplication.getInstance().userManager.user = userInfo
                ReflectUtils.navigationToHome(mView.context, 0)
                mView.context.finish()
            }

            override fun onFailure(call: Call, e: java.io.IOException) {
                // 处理错误情况，例如打印错误码和消息等
//                mView.context.runOnUiThread {
//                    mView.loginError(e.message)
//                }
                //测试数据
                val token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJyblN0ciI6IlNGMHBFUWp6MWRYc0swNkY0dExsd3NRbFN5cXhkUUVDIn0.VEq1Y_ZPeBeTR412szW2zjwFmcmdu4t8BJMz1YAtNHE"
                BaseApplication.getInstance().updateToken(token)
                val userInfo = UserInfo()
                userInfo.userId = ""
                userInfo.userName = ""
                userInfo.userAccount = userPhone
                BaseApplication.getInstance().userManager.user = userInfo
                ReflectUtils.navigationToHome(mView.context, 0)
                mView.context.finish()
            }
        })
    }
}